﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.IO;
using System.Text.RegularExpressions;

namespace 易览漫画
{
    public partial class Editing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.initial();
            }
        }

        private void initial()
        {
            string id = Request["id"];

            if (id == "")
            {
                Response.Redirect("AllBooks.aspx");
            }
            else
            {

                MySqlConnection conn = new MySqlConnection("Data Source=127.0.0.1;Initial Catalog=test;User ID=root;Password=123456");

                conn.Open();

                    if (conn.State.ToString() == "Open")
                    {
                        try
                        {
                            MySqlDataReader reader;
                            MySqlCommand com = new MySqlCommand();
                            com.Connection = conn;
                            com.CommandText = "select * from yilanmanhua where id=" + id;
                            reader=com.ExecuteReader();
                            while (reader.Read()) 
                            {
                                this.tushufenlei.SelectedValue = reader["fenlei"].ToString();
                                this.tushuzuozhe.Text = reader["zuozhe"].ToString();
                                this.tushumingcheng.Text=reader["mingcheng"].ToString();
                               
                                this.chubanriqi.Text=reader["riqi"].ToString();
                                this.tushujiage.Text=reader["jiage"].ToString();
                                this.manhuajianjie.Text=reader["jianjie"].ToString();
                              

                            }
                            conn.Close();
                        }
                        catch (MySqlException e1)
                        {
                            conn.Close();
                            Response.Write(e1.Message);

                        }


                }
                
            
            }


        }

        protected void submit_Click(object sender, EventArgs e)
        {
            string fenlei = this.tushufenlei.SelectedValue;
            string id = Request["id"];
            string zuozhe = this.tushuzuozhe.Text;
            if (zuozhe == "")
            {
                Response.Write("<script >function window.onload() {alert('请填写作者信息!');}</script>");
                return;
            }

            string mingcheng = this.tushumingcheng.Text;
            if (mingcheng == "")
            {
                Response.Write("<script >function window.onload() {alert('请填写图书名称!');}</script>");
                return;
            }
           
            string riqi = this.chubanriqi.Text;
            DateTime datett;
            bool b = DateTime.TryParse(riqi, out datett);
            if (riqi == "" || !b)
            {
                Response.Write("<script >function window.onload() {alert('出版日期错误!');}</script>");
                return;
            }
            string jiage = this.tushujiage.Text;
            decimal jia;
            bool bb = Decimal.TryParse(jiage, out jia);

            if (jiage == "" || !bb)
            {
                Response.Write("<script >function window.onload() {alert('请填写图书价格!');}</script>");
                return;
            }
            string jianjie = this.manhuajianjie.Text;
            if (jianjie == "")
            {
                Response.Write("<script >function window.onload() {alert('请填写图书简介!');}</script>");
                return;
            }

           

            if (this.fengmiantupian.HasFile)
            {
                string filetype = this.fengmiantupian.PostedFile.ContentType;
                if (filetype == "image/x-png")
                {
                    //有图片
                }
                else
                {
                    Response.Write("<script >function window.onload() {alert('图片格式需为png!');}</script>");
                    return;
                }

            }
            else
            {
                
            }

            if (this.tushupdf.HasFile)
            {

                string filetype = this.tushupdf.PostedFile.ContentType;
                if (filetype == "application/pdf")
                {
                    //有pdf文件
                }
                else
                {
                    Response.Write("<script >function window.onload() {alert('格式图书需为pdf!');}</script>");
                    return;
                }

            }
            else
            {
                
            }

            //接下来存到数据库中，并把文件存起来

            MySqlConnection conn = new MySqlConnection("Data Source=127.0.0.1;Initial Catalog=test;User ID=root;Password=123456");

            conn.Open();

            if (conn.State.ToString() == "Open")
            {
                try
                {

                    MySqlCommand com = new MySqlCommand();
                    com.Connection = conn;
                    com.CommandText = "update yilanmanhua set fenlei=?cfenlei,zuozhe=?czuozhe,mingcheng=?cmingcheng,riqi=?criqi,jiage=?cjiage,jianjie=?cjianjie where id=?cid";
                    com.Parameters.AddWithValue("cfenlei", fenlei);
                    com.Parameters.AddWithValue("czuozhe", zuozhe);
                    com.Parameters.AddWithValue("cmingcheng", mingcheng);
                  
                    com.Parameters.AddWithValue("criqi", datett);
                    com.Parameters.AddWithValue("cjiage", (float)jia);
                    com.Parameters.AddWithValue("cjianjie", jianjie);
                   
                    com.Parameters.AddWithValue("cid", id);
                    int i = com.ExecuteNonQuery();

                    if (i > 0)
                    {
                        if (this.fengmiantupian.HasFile)
                        {

                            //string picname = fengmiantupian.PostedFile.FileName;
                            //FileInfo file = new FileInfo(picname);
                            //string filename = file.Name;

                            string picpath = Server.MapPath("picture/" +id + ".png");
                            fengmiantupian.SaveAs(picpath);
                        }


                        if (this.tushupdf.HasFile)
                        {
                            string pdfpath = Server.MapPath("pdf/" +id + ".pdf");
                            tushupdf.SaveAs(pdfpath);
                        }

                    }
                    conn.Close();
                }
                catch (MySqlException e1)
                {
                    conn.Close();
                    Response.Write(e1.Message);

                }


            }


        }


    }
}